﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using Nancy.Model.DataModel.User;

namespace Nancy.Repository.EfRepository
{
   public class NancyDbContext:DbContext
    {
        public NancyDbContext() : base("Nancy")
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 去除表名复数
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            base.OnModelCreating(modelBuilder);
        }

        public  virtual  DbSet<User>User { get; set; }

        public  virtual  DbSet<UserInfo>UserInfo { get; set; }

        /// <summary>
        /// 保证当前线程中只有一个 DbContext 实例
        /// </summary>
        /// <returns></returns>
        public static DbContext CreateDbContext()
        {
            DbContext dbContext = (DbContext)CallContext.GetData("dbContext");
            if (dbContext == null)
            {
                dbContext = new NancyDbContext();

                CallContext.SetData("DbContext", dbContext);
            }
            return dbContext;
        }
    }
}
